import csv
from pdb import set_trace

"""
Function to read text files exported by AEWin.
"""

def read_statistics(fname):
    """
    Function to read text file exported by AEWin using the menu
    Statistics.
    fname - string name of file
    returns - dictionary of data
    """
    with open(fname, mode='rU') as f:
        contents = f.readlines()
    hit_start = contents.index('HIT DRIVEN DATA:\n') + 1
    time_start = contents.index('TIME DRIVEN DATA:\n') + 1
    hit_end = time_start - 2

    data = [a.strip().split(' ') for a in contents[hit_start:hit_end]]
    data = [filter(lambda a: a!='', r) for r in data]
    hit_data = [{a[0]:float(a[4]) for a in data[1:] if a[1]=='1'}]
    hit_data[0].update({'channel':'1', 'file':fname})
    hit_data.append({a[0]:float(a[4]) for a in data[1:] if a[1]=='2'})
    hit_data[1].update({'channel':'2', 'file':fname})
    return hit_data
